import Buff from "@/Buff";
import BuffUtil from "@/BuffUtil";
import ColorStr from "@/ColorStr";
import ObjectPool from "@/ObjectPool";
import SelectUtil from "@/SelectUtil";
import SolarBuffState from "@/SolarBuffState";
import { Frame } from "@/frame";
import { Trigger } from "@/trigger";

function formatTime(time: number) {
  let t = time / 1000;
  return `${Math.floor(t)}.${Math.floor((t % 1) * 10)}`;
}

export default class Buff界面 {
  static 提示UI: Frame;
  static buff数据: { [id: string]: Buff } = {};
  static 缓存池 = new ObjectPool<Frame[]>(Buff界面.创建BuffUI);
  static 创建BuffUI() {
    let size = 0.025;
    let art = Frame.createBackDrop().setSize(size, size).setTexture("");
    let stack = Frame.createTEXT(art.current)
      .setSize(size, size)
      .setAllPoints(art.current)
      .setText("")
      .setFont(0.01, "fonts\\fonts.ttf");
    stack.setTextAlignment(TEXTALIGN_CENTER);
    let duration = Frame.createTEXT(art.current)
      .setSize(size, 0.01)
      .setPoint(FRAMEPOINT_TOPLEFT, art.current, FRAMEPOINT_TOPLEFT, 0, 0.01)
      .setText("")
      .setFont(0.01, "fonts\\fonts.ttf");
    duration.setTextAlignment(TEXTALIGN_CENTER);
    const result = [art, stack, duration];
    return result;
  }
  constructor() {
    SolarBuffState.config.showSolarBuffUI = false;
    this.创建提示UI();

    let trigger = new Trigger();
    trigger.registerTimerEvent(0.1, true);
    trigger.addAction(this.刷新buff);
  }
  创建提示UI() {
    Buff界面.提示UI = Frame.createTEXT()
      .setFont(0.012, "fonts\\fonts.ttf")
      .setSize(0.22, 0);
    let bg = new Frame("BACKDROP", null, null, "_sl_border_backdrop", 0);
    bg.setParent(Buff界面.提示UI.current).setPoints(
      Buff界面.提示UI.current,
      0.01,
      0.01
    );
    Buff界面.提示UI.setVisible(false);
  }
  static 显示提示() {
    let h = DzGetTriggerUIEventFrame();
    let buff = Buff界面.buff数据["f" + h];
    if (buff) {
      let 提示文本 = ColorStr.yellow + (buff.getName() ?? "") + "|n";
      if (buff.stackCount && buff.stackCount != 1) {
        提示文本 =
          提示文本 +
          ColorStr.green +
          "层数: " +
          (buff.stackCount ?? "1") +
          "|n";
      }
      提示文本 = 提示文本 + ColorStr.white + (buff.getUbertip() ?? "");
      Buff界面.提示UI
        .setText(提示文本)
        .setVisible(true)
        .setPoint(FRAMEPOINT_BOTTOM, h, FRAMEPOINT_TOP, 0, 0.02);
    }
  }
  static 隐藏提示() {
    Buff界面.提示UI.setVisible(false);
  }
  刷新buff(this: void) {
    let 目标 = SelectUtil.getRealSelectUnit();
    if (目标 == null) {
      Buff界面.清空buffUI();
      return;
    }
    let 目标buff集合 = BuffUtil.getUnitBuffs(目标);
    if (目标buff集合 == null) {
      Buff界面.清空buffUI();
      return;
    }
    // 逐个显示buff
    let 池 = Buff界面.缓存池;
    池.setAllIdleStatus(true);
    let index = 0;
    let x位置 = 0.215;
    for (let buff种类Id in 目标buff集合) {
      let buff = 目标buff集合[buff种类Id];
      // 排除不显示的buff
      if (
        buff.buffType.class &&
        buff.buffType.class != "基础" &&
        buff.buffType.class != "羁绊"
      ) {
        continue;
      }
      // 渲染要显示的buff
      let ui组 = 池.borrowObject();
      let [图标ui, 层数ui, 持续时间ui] = ui组;
      图标ui.setVisible(true);
      图标ui.setTexture(buff.buffType.art);
      图标ui.setAbsPoint(FRAMEPOINT_BOTTOMLEFT, x位置 + index * 0.027, 0.14);
      if (buff.buffType.stackCountMax && buff.buffType.stackCountMax != 0) {
        层数ui.setText(buff.stackCount + "");
      } else {
        层数ui.setText("");
      }
      let 持续时间 =
        buff.buffType.class == "光环" || buff.buffType.class == "永久"
          ? "N/A"
          : formatTime(buff.getRemainingTime());
      if (buff.buffType.dur == null) {
        持续时间 = "N/A";
      }
      持续时间ui.setText(持续时间);
      index++;
    }

    池.forDirtyObjects((frames) => {
      frames[0].visible = false;
    });
  }
  static 清空buffUI() {
    Buff界面.缓存池.forActiveObjects((f) => (f[0].visible = false));
  }
}
